package com.atguigu.spzx.manager.task;

import cn.hutool.core.date.DateUtil;
import com.atguigu.spzx.manager.mapper.OrderInfoMapper;
import com.atguigu.spzx.manager.mapper.OrderStatisticsMapper;
import com.atguigu.spzx.model.entity.order.OrderStatistics;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: Shylo Sun
 * @Project: spzx-parent
 * @Date: 2023/11/19 16:14
 * 订单数据统计
 */
@Component
@Slf4j
public class OrderStatisticsTask {

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Autowired
    private OrderStatisticsMapper orderStatisticsMapper;

    // 每天凌晨两点查询前一天统计数据，把统计数据添加到统计结果里面
    // @Scheduled(cron = "0/10 * * * * ?") // 10s执行一次（测试用）
    @Scheduled(cron = "0 0 2 * * ?")
    public void orderTotalAmountStatistics() {
        // 1.获取前一天日期
        String createTime = DateUtil.offsetDay(new Date(), -1).toString("yyyy-MM-dd");
        // 2.根据前一天日期进行统计功能（分组操作）
        // 统计前一天交易金额
        OrderStatistics orderStatistics = orderInfoMapper.selectOrderStatistics(createTime);
        // 3.把统计之后的数据，添加至统计表里面
        if (orderStatistics != null) {
            orderStatisticsMapper.insert(orderStatistics);
        }
    }

}
